#include "thread_pool.h"

//将新连接插入队列
int insertConnect(int sockfd, conn_status* conns_st)
{
    //创建连接
    conn_record *record = (conn_record*)calloc(1, sizeof(conn_record));
    if(!record)
    {
        perror("calloc");
        return -1;
    }
    time(&record->lastTime);
    record->sockfd = sockfd;
    
    //获取当前时间索引
    int curIndex = record->lastTime%30;
    int targetIndex = (curIndex - 1 + TABLE_SIZE) % TABLE_SIZE;

    //更新连接记录槽位
    conns_st->index[sockfd] = targetIndex;

    //插入目标槽位
    if(conns_st->table[targetIndex] == NULL)
    {
        conns_st->table[targetIndex] = record;
        record->next = NULL;
    }
    else
    {
        record->next = conns_st->table[targetIndex];
        conns_st->table[targetIndex] = record;
    }
    return 0;
}

int updateConnect(int sockfd, conn_status *conns_st)
{
    int oldIndex = conns_st->index[sockfd];
    if(oldIndex == -1)
    {//连接未找到
        return -1;
    }

    conn_record* tmp = conns_st->table[oldIndex];
    conn_record* pre = NULL;
    conn_record* update = NULL;

    //遍历队列
    while(tmp != NULL)
    {
        if(tmp->sockfd == sockfd)
        {
            update = tmp;//获取到更新节点
            if(pre == NULL)
            {//为链表头结点
                conns_st->table[oldIndex] = tmp->next;
            }
            else
            {
                pre->next = tmp->next;
            }
            break;
        }
        pre = tmp;
        tmp = tmp->next;
    }

    if(update == NULL)
    {
        return -1;
    }
    int newIndex = (update->lastTime % TABLE_SIZE + TABLE_SIZE) % TABLE_SIZE;
    conns_st->index[sockfd] = newIndex;

    if (conns_st->table[newIndex] == NULL) {
        conns_st->table[newIndex] = update;
        update->next = NULL;
    } else {
        update->next = conns_st->table[newIndex];
        conns_st->table[newIndex] = update;
    }

    return 0;
}

int deleteConnect(int sockfd, conn_status* conns_st) {
    int oldIndex = conns_st->index[sockfd];
    if (oldIndex == -1) {
        return -1; // 连接未找到
    }

    conn_record* temp = conns_st->table[oldIndex];
    conn_record* prev = NULL;
    conn_record* conn = NULL;

    while (temp != NULL) {
        if (temp->sockfd == sockfd) {
            conn = temp;
            if (prev == NULL) {
                conns_st->table[oldIndex] = temp->next;
            } else {
                prev->next = temp->next;
            }
            break;
        }
        prev = temp;
        temp = temp->next;
    }

    if (conn == NULL) {
        return -1; // 连接未找到
    }

    conns_st->index[sockfd] = -1;
    free(conn);
    return 0;
}


int checkConnect(int epoll_fd, conn_status* conns_st) {
    time_t currTime;
    time(&currTime);

    int index = currTime % TABLE_SIZE;
    conn_record* temp = conns_st->table[index];
    conn_record* prev = NULL;

    while (temp != NULL) {
        if (currTime - temp->lastTime >= 30) {
            conn_record* toRemove = temp;
            if (prev == NULL) {
                conns_st->table[index] = temp->next;
            } else {
                prev->next = temp->next;
            }

            delEpollReadfd(epoll_fd, toRemove->sockfd);
            close(toRemove->sockfd);
            conns_st->index[toRemove->sockfd] = -1;
            free(toRemove);
        } else {
            prev = temp;
            temp = temp->next;
        }
    }

    return 0;
}

void initConnStatus(conn_status* conns_st) {
    for (int i = 0; i < TABLE_SIZE; i++) {
        conns_st->table[i] = NULL;
        conns_st->index[i] = -1;
    }
}

void destroyConnStatus(conn_status* conns_st) {
    for (int i = 0; i < TABLE_SIZE; i++) {
        conn_record* temp = conns_st->table[i];
        while (temp != NULL) {
            conn_record* toDelete = temp;
            temp = temp->next;
            free(toDelete);
        }
    }
}
